<template>
    <sc-title title="质量指标列表"></sc-title>
    <sc-form-table v-model="dataList" :addTemplate="addTemplate" drag-sort placeholder="暂无数据" copyExcel
        :excelMap="excelMap">
        <template #tableContent>
            <el-table-column prop="name" label="类别名称" width="200">
                <template #default="scope">
                    <el-input v-model="scope.row.name" placeholder="请输入类别名称"></el-input>
                </template>
            </el-table-column>
            <el-table-column prop="remark" label="包装要求" width="600">
                <template #default="scope">
                    <el-input v-model="scope.row.remark" type="textarea" :rows="3"></el-input>
                </template>
            </el-table-column>
            <el-table-column prop="status" label="状态">
                <template #default="scope">
                    <el-switch v-model="scope.row.status" active-value="normal" inactive-value="disable">
                    </el-switch>
                </template>
            </el-table-column>

        </template>
    </sc-form-table>

    <div class="button-position" style="margin-top:20px">
        <el-button @click="updateVisible">取 消</el-button>
        <el-button type="primary" @click="addPackreq">确 定</el-button>
    </div>
</template>
<script setup>
import { reactive, ref, onMounted, watch } from 'vue';
import { ElNotification } from 'element-plus';
import { Check, Close } from '@element-plus/icons-vue';
import Api from '@/api/model/pm'
import scFormTable from '@/components/scFormTable/index.vue'
import scTitle from '@/components/scTitle/index.vue'
import { deepClone } from '@/utils/arr';
const emit = defineEmits(["updatetable"]);
let optType = 'add'

const props = defineProps({
    paramsData: {
        type: Object,
        defalut: {},
    },
});
const form = ref({})
const dataList = ref([])
const addTemplate = ref({})
const excelMap = {
    name: '',
    remark: '',
    status: 'normal',
    number: ''
}
onMounted(() => {
    if (props.paramsData.optType) {
        optType = props.paramsData.optType
    }
    getInit();
})
const getInit = async () => {
    let res
    if (optType === 'add') {
        res = await Api.packReq.add.post();
    }
    else {
        res = await Api.packReq.detail.post(props.paramsData);
    }
    console.log(res)
    if (res.code == "200") {
        addTemplate.value = res.dtl;
        dataList.value = res.dataList || [deepClone(res.dtl)]
    }
}
const updateVisible = (val) => {
    emit("updatetable");
};
const addPackreq = async () => {
    let params = {
        dataList: dataList.value.filter(ele => { return ele.name })
    }
    let res = await Api.packReq.save.post(params)
    if (res.code == 200) {
        ElNotification.success({
            title: "",
            message: "操作成功！",
            duration: 2000,
        });
        emit("updatetable", true);
    }

}
</script>